package com.cskaoyan.service.wx.topic;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.top.TopicDetailVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: erdonc
 * @date: 2022/9/11 17:27
 * TODO:
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {

    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;


    /**
     * @Author: erdonc
     * @date : 2022/9/11 17:29
     * TODO:显示专题页面
     */
    @Override
    public CommonData<MarketTopic> list(BasePageInfo pageInfo) {
        // 开启分页
        PageHelper.startPage(pageInfo.getPage(), pageInfo.getLimit());
        // 设置排序条件
        pageInfo.setSort("add_time");
        pageInfo.setOrder("desc");

        // 添加查询条件
        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause(pageInfo.getSort() + " " + pageInfo.getOrder());
        MarketTopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        List<MarketTopic> marketTopicList = topicMapper.selectByExample(example);

        PageInfo<MarketTopic> topicPageInfo = new PageInfo<>(marketTopicList);
        return CommonData.data(topicPageInfo);
    }

    /**
     * @Author: erdonc
     * @date : 2022/9/11 20:50
     * TODO:根据页面请求id，查询专题信息和专题相关商品信息
     */
    @Override
    public TopicDetailVo detail(Integer id) {
        // 查询topic
        MarketTopic topic = topicMapper.selectByPrimaryKey(id);

        // 查询goods，根据topic中得到的goods[]查询对应的商品信息
        List<MarketGoods> goods = new ArrayList<>();
        Integer[] goodsIds = topic.getGoods();
        for (Integer goodsId : goodsIds) {
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(goodsId);
            goods.add(marketGoods);
        }

        // 对查询到的数据封装
        TopicDetailVo topicDetailVo = new TopicDetailVo(topic, goods);
        return topicDetailVo;
    }


    /**
     * @Author: erdonc
     * @date : 2022/9/11 21:20
     * TODO: 获取专题相关推荐
     */
    @Override
    public CommonData<MarketTopic> related(Integer id) {
        // 设置分页信息和排序信息，以及每页显示五条信息
        BasePageInfo pageInfo = new BasePageInfo();
        pageInfo.setPage(1);
        pageInfo.setLimit(5);
        pageInfo.setSort("add_time");
        pageInfo.setOrder("desc");

        // 调用list方法，把分页设置项传进去
        CommonData<MarketTopic> commonData = list(pageInfo);

        List<MarketTopic> commonDataList = commonData.getList();
        // 判断topic的Id和传进来的id是否相等
        int count = 0;
        for (MarketTopic topic : commonDataList) {
            if (topic.getId().equals(id)) {
                break;
            }
            count++;
        }
        // 从commonDataList中删除自己
        commonDataList.remove(count);

        return commonData;
    }
}
